Chess Challenges
Computer programs that play chess, solve chess puzzles, or create chess problems are widespread. There is even an annual contest pitting chess playing programs against each other and against human experts. This case study addresses a simple problem related to chess.
Background
The game of chess is played on an 8-by-8 board. The most powerful chess piece is the queen. From a given position, a queen may move anywhere along the same row, column, or diagonal, provided only that it doesn't jump over pieces. The shaded squares in the figure below illustrate the possible queen moves.
Two queens are said to attack each other if either can move to the position of the other - if they are in the same row, column, or diagonal, and no pieces lie between. The figure below shows examples of attacking and non attacking queens.
A puzzle problem dating from the 1700s is to place as many non attacking queens on the chessboard as possible. In this case study, we will design part of the solution to this problem: code to check if any of a set of queens attack one another.
Problem Statement
Design a type to represent a chessboard on which each square either is empty or contains a queen. Also design and test functions with the following headers and descriptions.
def ReadBoard(board):
ReadBoard asks the user to indicate which positions on the board contain queens. (ReadBoard may do this in any manner and need not check for invalid input.)
def PrintBoard(board):
PrintBoard prints the contents of the board as eight lines of eight characters each; the character 'Q' represents a square on the board that contains a queen, and the character '.' represents an empty board square.
def Safe(board):
Safe returns true exactly when the board contains a "safe" position, one in which no two queens on the board attack each other. Two queens attack each other exactly when they lie in the same row, column, or diagonal, and no pieces lie between them on the diagonal.
Each subprogram, except for its use of BoardType, should be independent of the remainder of the program.
Analysis
9.1 How many possibilities are there for two queens on the board to attack each other?
Analysis
9.2 How many diagonals are there on a chess board?
Application
9.3 How many queens can be placed on the chessboard so that none of them attacks one another?